package com.yuanqi.Controller;

import com.yuanqi.Common.Pojo.entity.Image;
import com.yuanqi.Return.Result;
import com.yuanqi.Service.PostService;
import com.yuanqi.Utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@Slf4j
@CrossOrigin
public class UplodeCommon {
    @Resource
    AliOssUtil aliOssUtil;
    @Resource
    PostService postService;
    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @PostMapping("/v1/upload")
    public Result uplode(@RequestParam("file") MultipartFile file) throws IOException, InterruptedException {
        log.info("{}上传了", file.getOriginalFilename());
        String string = UUID.randomUUID().toString();
        String filename = string + file.getOriginalFilename();
        String upload = null;
        try {
            upload = aliOssUtil.upload(file.getBytes(), filename);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Image image = postService.SaveImage(upload);
        return Result.success(image);
    }
}
